package com.oig.sys.base.app.spider;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.oig.common.constants.CommonConstants;
import com.oig.sys.base.constants.GoodsEnums;
import com.oig.sys.base.dto.HsGoodsDto;
import com.oig.sys.base.service.ITbHsGoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
public class GwHsCodePipeline implements Pipeline {

    private final ITbHsGoodsService hsGoodsService;


    @Override
    public void process(ResultItems resultItems, Task task) {
        log.debug("--process--");
        List<String> mainList = resultItems.get("mainList");
        List<String> taxList = resultItems.get("taxList");
        List<String> jgList = resultItems.get("jgList");
        List<String> jyList = resultItems.get("jyList");
        HsGoodsDto hsGoodsDto = this.updateHsGoods(mainList, taxList, jgList, jyList);
        hsGoodsDto.setRemark(DateUtil.formatDate(new Date())+"爬虫获取");
        this.hsGoodsService.updateSpiderHsGoods(hsGoodsDto);
    }


    private HsGoodsDto updateHsGoods(List<String> mainList, List<String> taxList,List<String> jgList,List<String> jyList){
        String trfPx = "元/", perPx="%";
        HsGoodsDto hsGoodsDto = new HsGoodsDto();
        hsGoodsDto.setHsCode(mainList.get(0).trim());   //hscode
        String hsName = mainList.get(1);    //商品名称
        if (hsName!=null&&hsName.indexOf("(")>0){
            hsGoodsDto.setName(hsName.trim().substring(0,hsName.indexOf("(")));
        } else if (hsName!=null){
            hsGoodsDto.setName(hsName.trim());
        }
        hsGoodsDto.setGoodsDesc(mainList.get(2).trim());//商品描述
        hsGoodsDto.setStatus(mainList.get(3).trim());
        hsGoodsDto.setGwUpdateDate(mainList.get(4));//网站最后更新日期
        String unitName = taxList.get(0);   //计量单位
        if (unitName!=null)hsGoodsDto.setUnit1(unitName.trim());
        String exTaxRate = taxList.get(1);  //出口税率
        if (exTaxRate!=null&&exTaxRate.indexOf(perPx)>0){
            String exTaxRateNum = exTaxRate.trim().substring(0,exTaxRate.indexOf(perPx));
            if (NumberUtil.isNumber(exTaxRateNum)) hsGoodsDto.setExportRate(new BigDecimal(exTaxRateNum));
        }
        String exRebateRate = taxList.get(2);   //出口退税税率
        if (exRebateRate!=null&&exRebateRate.indexOf(perPx)>0){
            String exRebateRateNum = exRebateRate.trim().substring(0,exRebateRate.indexOf(perPx));
            if (NumberUtil.isNumber(exRebateRateNum)) hsGoodsDto.setRebateRate(new BigDecimal(exRebateRateNum));
        }
        String exProvTaxRate = taxList.get(3);  //出口暂定税率
        if (exProvTaxRate!=null&&exProvTaxRate.indexOf(perPx)>0){
            String exProvTaxRateNum = exProvTaxRate.trim().substring(0,exProvTaxRate.indexOf(perPx));
            if (NumberUtil.isNumber(exProvTaxRateNum)) hsGoodsDto.setExportProvTaxRate(new BigDecimal(exProvTaxRateNum));
        }
        String vatRate = taxList.get(4);    //增值税
        if (vatRate!=null&&vatRate.indexOf(perPx)>0) {
            String vatRateNum = vatRate.trim().substring(0,vatRate.indexOf(perPx));
            if (NumberUtil.isNumber(vatRateNum)) hsGoodsDto.setVatRate(new BigDecimal(vatRateNum));
        }
        String hsYTax = taxList.get(5); //最惠国税率
        if (hsYTax!=null){
            if (hsYTax.indexOf(trfPx)>0){
                hsGoodsDto.setTariffType(GoodsEnums.TariffType.WEIGHT.getCode());
                String hsYTaxNum = hsYTax.trim().substring(0,hsYTax.indexOf(trfPx));
                if (NumberUtil.isNumber(hsYTaxNum)) hsGoodsDto.setOffTariffRate(new BigDecimal(hsYTaxNum).multiply(new BigDecimal(100)));
            } else if (hsYTax.indexOf(perPx)>0){
                hsGoodsDto.setTariffType(GoodsEnums.TariffType.PRICE.getCode());
                String hsYTaxNum = hsYTax.trim().substring(0,hsYTax.indexOf(perPx));
                if (NumberUtil.isNumber(hsYTaxNum)) hsGoodsDto.setOffTariffRate(new BigDecimal(hsYTaxNum));
            }
        }
        String provTaxRate = taxList.get(6); //进口暂定税率
        if (provTaxRate!=null){
            if (provTaxRate.indexOf(trfPx)>0){
                String provTaxRateNum = provTaxRate.trim().substring(0,provTaxRate.indexOf(trfPx));
                if (NumberUtil.isNumber(provTaxRateNum)) hsGoodsDto.setProvTaxRate(new BigDecimal(provTaxRateNum).multiply(new BigDecimal(100)));
            } else if (provTaxRate.indexOf(perPx)>0){
                String provTaxRateNum = provTaxRate.trim().substring(0,provTaxRate.indexOf(perPx));
                if (NumberUtil.isNumber(provTaxRateNum)) hsGoodsDto.setProvTaxRate(new BigDecimal(provTaxRateNum));
            }
        }
        String commTariffRate = taxList.get(7); //进口普通税率
        if (commTariffRate!=null){
            if (commTariffRate.indexOf(trfPx)>0){
                if (StringUtils.isBlank(hsGoodsDto.getTariffType()))  hsGoodsDto.setTariffType(GoodsEnums.TariffType.WEIGHT.getCode());
                String commTariffRateNum = commTariffRate.trim().substring(0,commTariffRate.indexOf(trfPx));
                if (NumberUtil.isNumber(commTariffRateNum)) hsGoodsDto.setCommTariffRate(new BigDecimal(commTariffRateNum).multiply(new BigDecimal(100)));
            } else if (commTariffRate.indexOf(perPx)>0){
                if (StringUtils.isBlank(hsGoodsDto.getTariffType()))  hsGoodsDto.setTariffType(GoodsEnums.TariffType.PRICE.getCode());
                String commTariffRateNum = commTariffRate.trim().substring(0,commTariffRate.indexOf(perPx));
                if (NumberUtil.isNumber(commTariffRateNum)) hsGoodsDto.setCommTariffRate(new BigDecimal(commTariffRateNum));
            }
        }
        String cmpTaxRate = taxList.get(8); //消费税率
        if (cmpTaxRate!=null){
            if (cmpTaxRate.indexOf(trfPx)>0){
                String cmpTaxRateNum = cmpTaxRate.trim().substring(0,cmpTaxRate.indexOf(trfPx));
                if (NumberUtil.isNumber(cmpTaxRateNum)) hsGoodsDto.setCmpTaxRate(new BigDecimal(cmpTaxRateNum).multiply(new BigDecimal(100)));
            } else if (cmpTaxRate.indexOf(perPx)>0){
                String cmpTaxRateNum = cmpTaxRate.trim().substring(0,cmpTaxRate.indexOf(perPx));
                if (NumberUtil.isNumber(cmpTaxRateNum)) hsGoodsDto.setCmpTaxRate(new BigDecimal(cmpTaxRateNum));
            }
        }
        hsGoodsDto.setMonitor(String.join(CommonConstants.RADICAL,jgList));
        hsGoodsDto.setIqType(String.join(CommonConstants.DOT_STR,jyList));
        log.debug("spider Page process:{}", hsGoodsDto);
        return hsGoodsDto;
    }



}
